<template>
  <div>
    <el-breadcrumb separator="/" style="padding-left:10px;padding-bottom:10px;font-size:12px;">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>日志管理</el-breadcrumb-item>
      <el-breadcrumb-item>登录日志</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-form :inline="true" :model="queryKeyWord" class="demo-form-inline">
        <el-form-item label="用户名" label-width="70px">
          <el-input clearable v-model="queryKeyWord.keyWord" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item style="margin-left: 10px">
          <el-button icon="el-icon-refresh" @click="resetLoginLogVo">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="getLoginLodList">查询</el-button>
        </el-form-item>
      </el-form>
      <!-- 表格 -->
      <el-table
        :data="loginLogList"
        border
        max-height="100%"
        style="width: 100%;">
        <el-table-column
          prop="id"
          label="#"
          width="50">
        </el-table-column>
        <el-table-column
          prop="username"
          label="用户名"
          width="110">
        </el-table-column>
        <el-table-column
          prop="loginTime"
          sortable
          label="登录时间"
          width="180">
        </el-table-column>
        <el-table-column
          sortable
          prop="location"
          label="登录地点"
          width="350">
        </el-table-column>
        <el-table-column
          prop="ip"
          label="IP地址"
          width="150">
        </el-table-column>
        <el-table-column
          prop="userSystem"
          label="操作系统"
          width="150">
        </el-table-column>
        <el-table-column
          prop="userBrowser"
          label="浏览器"
          width="150">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scop">
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="del(scop.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="padding-top: 15px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"

        :current-page="current"
        :page-sizes="[ 10, 20, 30,60]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { deleteLoginLog, getLoginLodList } from '@/api/Users'

export default {
  name: 'LoginLog',
  data(){
    return {
      queryKeyWord:{
        keyWord:''
      },
      //每页显示的条数
      size: 10,
      //总条数
      total: 100,
      //当前页码
      current: 1,
      loginLogList:[],
    }
  },
  mounted () {
    this.getLoginLodList()
  },
  methods:{
    //当每页条数改变的时候
    handleSizeChange (val) {
      //将val赋值到size
      this.size = val;
      //重新到后台查询
      this.getLoginLodList();
    },
    //当前页码改变的时候
    handleCurrentChange (val) {
      this.current=val;
      this.getLoginLodList();
    },
    resetLoginLogVo(){
      this.queryKeyWord.keyWord=''
      this.getLoginLodList()
    },
    async getLoginLodList(){
      const { data } = await getLoginLodList(this.current,this.size,this.queryKeyWord)
      if (data.code ==200){
        this.loginLogList=data.data.records;
        this.total=data.data.total;
      }else {
        return this.$message.error("登录日志查询失败:" + data.msg);
      }
    },
    async del(id){
      var res = await this.$confirm(
        "此操作将永久删除该登录日志, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      });
      if (res == "confirm") {
        const { data: res } = await deleteLoginLog (id)
        if (res.code == 200) {
          this.$notify.success({
            title:'操作成功',
            message:'登录日志删除成功',
          });
          this.getLoginLodList();
        } else {
          this.$message.error(res.msg);
        }
      }
    }
  }
}
</script>

<style scoped>

</style>
